
package lucene.server.web.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.Data;
import lucene.server.common.model.Table;
import lucene.server.common.utils.JsonUtil;
import lucene.server.common.utils.StringUtil;

import java.util.Properties;

@Data
@Table(name = "cdc")
public class CDCConfig {

    /// <summary>
    /// 主键
    /// </summary>
    private String id;

    /// <summary>
    /// 名称
    /// </summary>
    private String name;

    /// <summary>
    /// 数据库类型
    /// </summary>
    private String type;

    /// <summary>
    /// 数据库服务器地址
    /// </summary>
    private String host;

    /// <summary>
    /// 服务器端口
    /// </summary>
    private String port;

    /// <summary>
    /// 用户名
    /// </summary>
    private String username;

    /// <summary>
    /// 密码
    /// </summary>
    private String password;

    /// <summary>
    /// 过滤数据库和表
    /// </summary>
    private String filter;

    /// <summary>
    ///  参数,json格式
    /// </summary>

    private String params;
    @JsonSetter("params")
    public void setParamsList(JsonNode paramsArray) {
        this.params = paramsArray.toString();
    }

    @JsonIgnore
    public Properties toProperties(){
        Properties properties = new Properties();
        properties.setProperty("name", this.name);
        properties.setProperty("host", this.host);
        properties.setProperty("port", this.port);
        properties.setProperty("type", this.type);
        properties.setProperty("username",this.username);
        properties.setProperty("password", this.password);
        properties.setProperty("filter", filter);
        if (StringUtil.isNotEmpty(params)){
            ArrayNode jsonArray = JsonUtil.getArrayNode(params);
            jsonArray.forEach(json->{
                if (StringUtil.isNotEmpty(json.get("key").asText())){
                    properties.setProperty(json.get("key").asText(),json.get("value").asText());
                }
            });
        }
        return properties;
    }

}
